package com.iptv.grape.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.SeekBar;

import com.iptv.grape.ui.control.INextFocus;
import com.iptv.grape.util.AppLog;


import java.util.ArrayList;

public class XSeekBar extends SeekBar {
    public XSeekBar(Context context) {
        super(context);
    }

    public XSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public XSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

//    public XSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            int keyCode = event.getKeyCode();
            // 19 UP
            // 20 DOWN
            // 21 LEFT
            // 22 R
            //this.getProgress();
            //AppLog.getCurLog().out(this.getProgress());
            if(this.getProgress() == 0 && keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
                return true;
            }
            if(this.getProgress() == this.getMax() && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
                return true;
            }
            if(keyCode == 20){
                triggerEvent();
                //nextRecycleView.getCurLabelHolderRequestFocus();
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }




    // 事件监听者的存放列表
    private ArrayList<INextFocus> eventListenerList = new ArrayList<INextFocus>();

    // 添加事件监听者
    public void setOnNextFocusListener(INextFocus nextFocus){
        eventListenerList.add(nextFocus);
    }
    // 移除事件监听者
    public void removeListener(INextFocus nextFocus){
        if(!eventListenerList.isEmpty()){
            eventListenerList.remove(nextFocus);
        }
    }

    private void triggerEvent() {
        try {
            for (INextFocus nextFocus : eventListenerList) {
                nextFocus.toNextFocus();
            }
        }
        catch (Exception e){

        }
    }

}
